<template>
  <a-form
    :model="formState"
    name="basic"
    :label-col="{ span: 8 }"
    :wrapper-col="{ span: 16 }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item
      label="mobile"
      name="mobile"
      :rules="[{ required: true, message: 'Please input your mobile!' }]"
    >
      <a-input v-model:value="formState.mobile" />
    </a-form-item>

    <a-form-item
      label="Password"
      name="password"
      :rules="[{ required: true, message: 'Please input your password!' }]"
    >
      <a-input-password v-model:value="formState.password" />
    </a-form-item>

    <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
      <a-button type="primary" html-type="submit">Submit</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { apiLogin } from "@/api/login";
import { useRouter } from 'vue-router';
import cookies from '@/utils/cookies';
const router = useRouter();

interface FormState {
  mobile: number | undefined;
  password: string;
}

const formState = reactive<FormState>({
  mobile: 123456,
  password: "123456",
});
const onFinish = (values: any) => {
  console.log("Success:", values);
  apiLogin({
    ...values,
    region: 91,
  }).then((res: any) => {
    localStorage.setItem("token", res.authorization);
    cookies.set("Authorization", res.authorization);
    router.replace('/');
  });
};

const onFinishFailed = (errorInfo: any) => {
  console.log("Failed:", errorInfo);
};
</script>
